package com.lsh.oms.core.utils;

import com.lsh.oms.core.redis.RedisTemplate;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class BatchNumberGenerator {

    @Resource
    private RedisTemplate redis;

    public String generateReturnRequestNumber(Long orderId) {
        String key = "order.return.request#" + orderId;
        String value = incr(key);
        expireKey(key);
        return value;
    }

    public String generateReturnNumber(Long orderId) {
        String key = "order.return#" + orderId;
        String value = incr(key);
        expireKey(key);
        return value;
    }

    public String generateReturnResultNumber(Long orderId) {
        String key = "order.return.result#" + orderId;
        String value = incr(key);
        expireKey(key);
        return value;
    }

    private String incr(String key) {
        return String.valueOf(redis.incr(key));
    }

    private void expireKey(String key) {
        redis.expireAt(key, new DateTime().plusMonths(2).toDate());
    }
}
